package com.kernal.demo.smartvision;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.kernal.smartvision.activity.PictureRecogActivity;
import com.kernal.smartvision.activity.SmartvisionCameraActivity;
import com.kernal.smartvision.activity.SmartvisionSettingActivity;
import com.kernal.smartvision.utils.Utills;
import com.kernal.smartvisionocr.R;


public class SmartvisionMainActivity extends AppCompatActivity {
    private int width, height;
    ImageButton btn_set, btn_app_logo, btn_scanRecog, btn_import;
    //扫描识别请求码
    private final static int SCAN_REQUEST_CODE = 101;
    //导入识别请求码
    private final static int IMPORT_REQUSET_CODE = 102;

    //识别结果
    private String recogResult;
    //识别成功后图片路径（默认为裁切图片）
    private String resultPic;
    //敏感区域图片路径
    private String uploadPicPath;
    //表示识别类型,0:vin码 1：手机号
    private int ocrType;
    /**
     * 扫描识别监听事件
     */
    private View.OnClickListener scanRecogOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            Intent cameraIntent = new Intent(getApplicationContext(), SmartvisionCameraActivity.class);
            startActivityForResult(cameraIntent, SCAN_REQUEST_CODE);
        }
    };

    /**
     * 导入识别监听事件
     */
    private View.OnClickListener importRecogOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            Intent intent = new Intent(getApplicationContext(), PictureRecogActivity.class);
            startActivityForResult(intent, IMPORT_REQUSET_CODE);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        com.kernal.smartvision.utils.Utills.initImageLoader(getApplicationContext());
        initView();
    }


    /**
     * 获取识别结果
     *
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            //扫描识别结果
            case SCAN_REQUEST_CODE:
                if (resultCode == RESULT_OK) {
                    setContentView(R.layout.activity_blank);
                    recogResult = data.getStringExtra("RecogResult");
                    resultPic = data.getStringExtra("resultPic");
                    uploadPicPath = data.getStringExtra("uploadPicPath");
                    ocrType = data.getIntExtra("ocrType", 0);
                    gotoResultPage(SCAN_REQUEST_CODE);
                }
                break;
            //导入识别结果
            case IMPORT_REQUSET_CODE:
                if (resultCode == RESULT_OK) {
                    recogResult = data.getStringExtra("RecogResult");
                    resultPic = data.getStringExtra("resultPic");
                    uploadPicPath = data.getStringExtra("uploadPicPath");
                    ocrType = data.getIntExtra("ocrType", 0);
                    gotoResultPage(IMPORT_REQUSET_CODE);
                }
                break;
            default:
                break;
        }
    }

    private ShowResultView showResultView;

    /**
     * 跳转识别结果界面
     */
    private void gotoResultPage(int scanType) {
        if (showResultView == null)
            showResultView = new ShowResultView(this);
        if (SCAN_REQUEST_CODE == scanType) {
            showResultView.showResultlayoutView(0, recogResult, ocrType, resultPic, uploadPicPath);
        } else if (IMPORT_REQUSET_CODE == scanType) {
            showResultView.showResultlayoutView(1, recogResult, ocrType, resultPic, uploadPicPath);
        }

       /* Intent intent = new Intent(getApplicationContext(),ShowResultActivity.class);
        intent.putExtra("RecogResult", recogResult);
        intent.putExtra("ocrType",ocrType);
        //裁切图
        intent.putExtra("resultPic",resultPic);
        //敏感区域图
        intent.putExtra("uploadPicPath",uploadPicPath);
        startActivity(intent);
        finish();*/
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (showResultView != null) {
                initView();
                showResultView = null;
                return false;
            }
            return super.onKeyDown(keyCode, event);
        }
        return super.onKeyDown(keyCode, event);

    }

    public void initView() {
        setContentView(R.layout.activity_smartvision);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        DisplayMetrics dm = new DisplayMetrics();
        this.getWindowManager().getDefaultDisplay().getMetrics(dm);
        width = dm.widthPixels;
        height = dm.heightPixels;

        btn_set = (ImageButton) findViewById(R.id.smartvision_btn_set);
        btn_app_logo = (ImageButton) findViewById(R.id.smartvision_btn_app_logo);
        btn_scanRecog = (ImageButton) findViewById(R.id.smartvision_btn_scanRecog);
        btn_import = (ImageButton) findViewById(R.id.smartvision_btn_import);

        //设置按钮
        btn_set.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent(getApplicationContext(), SmartvisionSettingActivity.class);
                startActivity(intent);
            }
        });

        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams((int) (width * 0.2), (int) (width * 0.2));
        params.addRule(RelativeLayout.CENTER_HORIZONTAL);
        params.topMargin = (int) (0.7 * height);
        btn_scanRecog.setLayoutParams(params);


        params = new RelativeLayout.LayoutParams((int) (width * 0.15), (int) (width * 0.15));
        params.leftMargin = (int) (width * 0.7);
        params.topMargin = (int) (0.85 * height);
        btn_import.setLayoutParams(params);

        params = new RelativeLayout.LayoutParams((int) (width * 0.4), (int) (height * 0.18));
        params.addRule(RelativeLayout.CENTER_HORIZONTAL);
        params.topMargin = (int) (0.2 * height);
        btn_app_logo.setLayoutParams(params);
        params = new RelativeLayout.LayoutParams((int) (width * 0.1), (int) (width * 0.1));
        params.leftMargin = (int) (width * 0.75);
        params.topMargin = (int) (0.02 * height);
        btn_set.setLayoutParams(params);
        /**
         * 扫描识别
         */
        btn_scanRecog.setOnClickListener(scanRecogOnClickListener);

        /**
         * 导入识别
         */
        btn_import.setOnClickListener(importRecogOnClickListener);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (Build.VERSION.SDK_INT > 18) {
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
            decorView.setSystemUiVisibility(option);
        }


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
